package com.hatic.money;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;

/**
 * 投资计划完成情况登记处理，处理明细金额汇总。
 *
 * @author TonyTan
 * @version 1.0, 2011-10-18
 */
public class PlanCompBO extends BusinessObject {
	private static final long serialVersionUID = 1L;

	//更新计划包的金额
	public String sumMoney(String parentId) {
		Map<String,String> mpSum = sumPlan(parentId);
		
		//取包属性，如果是大计划，则不更新“预计总投资”与“立项批复金额”的值
		String baoType = getBaoType(parentId);
		
		double plan_money = tonum(mpSum.get("plan_money"));
		double year_money = tonum(mpSum.get("year_money"));
		_log.showDebug("------------------投资计划合计金额为：" + plan_money + ";" + year_money);
		
		double money_pay = tonum(mpSum.get("money_pay"));
		double tran_money1 = tonum(mpSum.get("tran_money1"));
		double money_pay1 = tonum(mpSum.get("money_pay1"));
		double sun_money = tonum(mpSum.get("sun_money"));
		double sun_money1 = tonum(mpSum.get("sun_money1"));
		double tran_money2 = tonum(mpSum.get("tran_money2"));
		double project_money = tonum(mpSum.get("project_money"));
		double check_money = tonum(mpSum.get("check_money"));
		double money_contract = tonum(mpSum.get("money_contract"));
		double cost_money = tonum(mpSum.get("cost_money"));
		
		if (!checkmoney(plan_money, year_money)) return _returnFaild;
		
		String usql = "update money_plan set " +
					  "year_money = ?, " +
					  "money_pay = ?, tran_money1 = ?, " +
					  "money_pay1 = ?, sun_money = ?, sun_money1 = ?, " +
					  "tran_money2 = ? ,"+
					  "check_money = ? , money_contract = ?, cost_money = ? " ;
		
		//虚拟包才更新“预计总投资”与“立项批复金额”的值
		if (baoType.equals("1")) {
			usql += ", project_money = ?, plan_money = ? ";
		}
		usql += "where plan_id = ?";
		
		DaoParam uparam = _dao.createParam(usql);
		uparam.addDoubleValue(Double.toString(year_money));
		uparam.addDoubleValue(Double.toString(money_pay));
		uparam.addDoubleValue(Double.toString(tran_money1));
		uparam.addDoubleValue(Double.toString(money_pay1));
		uparam.addDoubleValue(Double.toString(sun_money));
		uparam.addDoubleValue(Double.toString(sun_money1));
		uparam.addDoubleValue(Double.toString(tran_money2));
		uparam.addDoubleValue(Double.toString(check_money));
		uparam.addDoubleValue(Double.toString(money_contract));
		uparam.addDoubleValue(Double.toString(cost_money));
		
		//虚拟包才更新“预计总投资”与“立项批复金额”的值
		if (baoType.equals("1")) {
			uparam.addDoubleValue(Double.toString(project_money));
			uparam.addDoubleValue(Double.toString(plan_money));
		}
		
		uparam.addStringValue(parentId);
		if (!_dao.update(uparam)) {
			setMessage("更新计划包的金额出错！");
			return _returnFaild;
		}
		
		return _returnSuccess;
	}
	
	//汇总子计划的金额：
	//预计总投资、本年度计划投资、已付款、今年截止统计月份实际付款
	//今年预计完成投资、截止今年底累计付款额、截止上年底累计付款、
	//今年剩余月份预计付款、立项批复金额
	//计划批复金额、合同金额
	//决算审计金额、国家局批复金额
	//上报金额、调概后批复金额
	//国家局批复年度投资、下年计划付款
	private Map<String,String> sumPlan(String parentId) {
		String sql = "select sum(plan_money) as plan_money, sum(year_money) as year_money, " +
				"sum(money_pay) as money_pay, sum(tran_money1) as tran_money1, " + 
				"sum(money_pay1) as money_pay1, sum(sun_money) as sun_money, sum(sun_money1) as sun_money1, " + 
				"sum(tran_money2) as tran_money2, sum(project_money) as project_money, " + 
				"sum(check_money) as check_money, sum(money_contract) as money_contract, " + 
				"sum(cost_money) as cost_money, sum(check_money2) as check_money2, " + 
				"sum(app_money) as app_money, sum(check_moneychk) as check_moneychk ," + 
				"sum(year_money2) as year_money2, sum(year_money1) as year_money1 " + 
				"from money_plan where plan_parentid = ? and plan_status < '91'";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(parentId);
		return _dao.queryMap(param);
	}
	
	//效验总投资与年度投资值的合法性
	private boolean checkmoney(double plan_money, double year_money) {
		if (plan_money == 0) {
			setMessage("当前计划包中的子计划预计总投资合计为0，不能提交！");
			return false;
		}
		if (year_money == 0) {
			setMessage("当前计划包中的子计划年度投资合计为0，不能提交！");
			return false;
		}
		if (plan_money < year_money) {
			setMessage("当前计划包中的子计划年度投资大于预计总投资，不能提交！");
			return false;
		}
		
		return true;
	}
	
	//字符串转换为数字
	private double tonum(String num) {
		if (num == null || num.length() == 0) return 0;
		
		return Double.parseDouble(num);
	}
	
	//取投资计划包属性
	private String getBaoType(String planId) {
		String sql = "select bao_type from money_plan where plan_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(planId);
		
		Map<String, String> mpData = _dao.queryMap(param);
		return mpData.get("bao_type");
	}
}
